# include <stdio.h>
# include <stdlib.h>

int *a,*b,size,sizeb,value,countv;
	
// To append in the main list

void append(int value)
	{
	a=realloc(a,sizeof(int)*(size+1));
	a[size]=value;
	size++;
	}

// To append in any extended list

void appendx(int value)
	{
	b=realloc(b,sizeof(int)*(sizeb+1));
	b[sizeb]=value;
	sizeb++;
	}

// To find the index of the given value

int index1(int value)
	{
	for(countv=0;countv<size;countv++)
		{
		if(a[countv]==value)
			return countv;	
		}
	printf("Element not found");	
	}

// To find number of occurences of given value

int count(int value)
	{
	countv=0;
	int i;
	for(i=0;i<size;i++)
		{
		if(a[i]==value)
			countv++;
		}
	return countv;	
	}

// To insert given element at given position

void insert(int value,int n)
	{
	a=realloc(a,sizeof(int)*(++size));
	int r;
	for(r=size;r>n;--r)
		a[r]=a[r-1];
	a[n]=value;
	}

// To find the maximun value in the given list

int max()
	{
	countv=a[0];
	int i;
	for(i=0;i<size-1;i++)
		{
		if(a[i]<a[i+1])
			countv=a[i+1];
		}
	return countv;
	}

// To find the minimum value in the given list

int min()
	{
	countv=a[0];
	int i;
	for(i=0;i<size-1;i++)
		{
		if(a[i]>a[i+1])
			countv=a[i+1];
		}
	return countv;
	}

// To reverse the given list
 	
void reverse()
	{
	int i=0,j=size-1,tmp;
	if(size%2)
		{		
		for(;i!=j;i++,j--)
			{
			tmp=a[i];
			a[i]=a[j];
			a[j]=tmp;
			}
		}
	else
		{
		for(i=0;i!=((size/2));i++,j--)
			{
			tmp=a[i];
			a[i]=a[j];
			a[j]=tmp;
			}
		}
	}

// To pop the last elemrnt of the list

int pop()
	{
	value=a[size];
	a=realloc(a,sizeof(int)*(size-1));
	size--;
	return value;
	}

// To remove given element of list

void remove1(int value)
	{
	int i,tmp;
	for(i=0;i<size;i++)
		{
		if(a[i]==value)
			break;
		}
	for(;i<size-1;i++)
		a[i]=a[i+1];
	a=realloc(a,sizeof(int)*(size-1));
	size--;
	}

// To display the list
		
void show()
	{
	int i;
	printf("[");
	for(i=0;i<size;i++)
		printf("%d ",a[i]);
	printf("]\n");
	}

// To sort the given list in ascending order
	
void sort()
	{
	int i,j,tmp;
	for(i=0;i<size;i++)
		{
		tmp=a[i];
		for(j=i;j<size;j++)
			{
			if(tmp>a[j])
				{				
				tmp=a[j];
				break;
				}			
			}
			a[j]=a[i];
			a[i]=tmp;
		}
	}

// To insert the given element at the beginning of the list

void push(int value)
	{
	a=realloc(a,sizeof(int)*(size+1));
	size++;
	int i=size-1;
	for(;i>=0;i--)
		a[i+1]=a[i];
	a[0]=value;
	}
// To insert given element at the given position

void insert1(int value,int countv)
	{
	a=realloc(a,sizeof(int)*(size+1));
	size++;
	int i=size-1;
	for(;i>=countv;i--)
		a[i+1]=a[i];
	a[countv]=value;
	}

// To extend the given list

void extend()
	{
	a=realloc(a,sizeof(int)*(sizeb));
	int tmp=size,i;
	size=size+sizeb;
	for(i=0;tmp<size,i<sizeb;tmp++,i++)
		a[tmp]=b[i];
	}


int  main(void)
	{
	int ans,choice,value,pos;
	
	do
		{
		free(a);
		free(b);
		size=0;
		sizeb=0;
		countv=0;
		value=0;
		do	{	
			printf("Enter data\n");
			scanf("%d",&value);
			append(value);
			printf("Do you want to enter more?(1/0)\n");
			scanf("%d",&ans);}while(ans);
		

		printf(" Enter operation number according to the menu \n");
		printf(" Find Length of the list :                    1\n");
		printf(" Find Maximum element of the list :           2\n");
		printf(" Find Minimum element of the list :           3\n");
		printf(" Find Reverse of the list :                   4\n");
		printf(" Find Count of the given element in the list: 5\n");
		printf(" Find Value using index in the list :         6\n");
		printf(" Pop last element of the list :               7\n");
		printf(" Append element into the list :               8\n");	
		printf(" Remove element from the list :               9\n");
		printf(" Extend a given list:	                      10\n");
		printf(" Push at the begining an element:             11\n");
		printf(" Sort the given list :			      12\n");
		printf(" Insert an element at given position:         13\n");
		printf(" Show List:				      14\n");	
				
		do{	
			printf("Enter choice\n");
			scanf("%d",&choice);
			switch(choice)
				{		
				case 1:{printf("%d\n",size);break;}

				case 2:{printf("%d\n",max());break;}

				case 3:{printf("%d\n",min());break;}

				case 4:{reverse();show();break;}

				case 5:{printf("Enter count value");scanf("%d",&value);printf("%d\n",count(value));break;}

				case 6:{printf("Enter index value");scanf("%d",&value);printf("%d\n",index1(value));break;}

				case 7:{pop();show();break;}

				case 8:{printf("Enter value to append");scanf("%d",&value);append(value);show();break;}

				case 9:{printf("Enter value to remove");scanf("%d",&value);remove1(value);show();break;}
				
				case 10:{
					do{	

						printf("Enter data\n");
						scanf("%d",&value);
						appendx(value);					
						printf("Do you want to enter more?(1/0)\n");
						scanf("%d",&ans);}while(ans);
					extend();
					show();
					break;
					}
						
				case 11:{
					
					printf("Enter value to push");scanf("%d",&value);
					push(value);
					show();
					break;
					}				

				case 12:{
					sort();
					printf("Do you want to sort in ascending(1) or descending(2) order?");
					scanf("%d",&choice);
					if(choice==1)
						show();
					else
						{
						reverse();
						show();
						}
					break;
					}				

				case 13:{
					printf("Enter element and position to insert at");	
					scanf("%d %d",&value,&pos);
					insert1(value,pos);
					show();
					break;
					}

				case 14:{show();break;}
				}

			printf("Do you want to repeat from given menu?(1/0)\n");
			scanf("%d",&ans);
		  }while(ans);
		
		printf("Do you want to exit or to start afresh?(1/0)\n");
		scanf("%d",&ans);
		}while(ans);
	return 0;}

	


	
